package com.mouse.magent.proxy.action;

import com.mouse.magent.common.handler.CommonHandler;
import com.mouse.magent.common.protocol.Message;
import com.mouse.magent.common.service.BaseAction;
import com.mouse.magent.common.util.NettyUtil;
import com.mouse.magent.proxy.constant.ChannelConstant;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

@Slf4j
public class RemoteDisconnectAction extends BaseAction {
    @Override
    public void handle(ChannelHandlerContext ctx, Message message) throws Exception {
        String remoteChannelId = message.getHeader().getChannelId();
        log.info("channelId:{} ip:{} remote disconnect", remoteChannelId, NettyUtil.getRemoteIp(ctx));
        Map<String, CommonHandler> channelHandlerMap = ChannelConstant.getChannelHandlerMap();
        CommonHandler commonHandler = channelHandlerMap.get(remoteChannelId);
        if (commonHandler != null) {
            commonHandler.getCtx().close();
            channelHandlerMap.remove(remoteChannelId);
        }
    }
}
